﻿using AutoMapper;
using MediatR;
using S.RBAC.Api.Application.Command.RoleCommand;
using S.RBAC.Domain.Model;
using S.RBAC.Infrastructure.Interface;

namespace S.RBAC.Api.Application.CommandHandlers.RoleHandler
{
    public class RoleCreateCommandHandler : IRequestHandler<RoleCreateCommand, int>
    {
        protected readonly IRoleRepository _role;
        protected readonly IMapper _mapper;

        public RoleCreateCommandHandler(IRoleRepository role, IMapper mapper)
        {
            _role = role;
            _mapper = mapper;
        }

        public async Task<int> Handle(RoleCreateCommand request, CancellationToken cancellationToken)
        {
            var info = await _role.GetRoleInfo(t => t.RoleName == request.RoleName);
            if (info.Count > 0)
            {
                return 0;
            }

            return await _role.AddRole(new Role() { RoleName = request.RoleName, RoleDescribe = request.RoleDescribe, CreateBy = "孔令琪", CreateDate = DateTime.Now, UpdateBy = "", UpdateDate = null, IsDel = false });
        }
    }
}
